package com.human.server.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.human.server.entity.Attendance;
import com.human.server.entity.AttendanceCheckin;
import com.human.server.model.R;
import com.human.server.model.request.AttendanceRequest;
import com.human.server.service.impl.AttendanceCheckinServiceImpl;
import com.human.server.service.impl.AttendanceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/human/attendance")
public class AttendanceController {

    @Autowired
    private AttendanceServiceImpl attendanceService;

    @Autowired
    private AttendanceCheckinServiceImpl attendanceCheckinService;

    /**
     * 分页查询
     * @param attendanceRequest
     * @return
     */
    @PostMapping("/page")
    public R page(@RequestBody AttendanceRequest attendanceRequest) {
        return R.success(attendanceService.page(attendanceRequest, new LambdaQueryWrapper<Attendance>()
                .eq(StrUtil.isNotEmpty(attendanceRequest.getName()), Attendance::getEmployeeName, attendanceRequest.getName())
                .eq(StrUtil.isNotEmpty(attendanceRequest.getMonth()), Attendance::getMonth, attendanceRequest.getMonth())
                .orderByDesc(Attendance::getCreateTime)
        ));
    }

    /**
     * 新增
     * @param attendance
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Attendance attendance) {
        return R.success(attendanceService.save(attendance));
    }

    /**
     * 修改
     * @param attendance
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody Attendance attendance) {
        return R.success(attendanceService.updateById(attendance));
    }

    /**
     * 删除
     * @param attendance
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Attendance attendance) {
        return R.success(attendanceService.removeById(attendance.getId()));
    }

    /**
     * 详情
     * @param attendance
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody Attendance attendance) {
        return R.success(attendanceService.getById(attendance.getId()));
    }

    /**
     * 列表
     * @return
     */
    @PostMapping("/list")
    public R list() {
        return R.success(attendanceService.list());
    }

    /**
     * upload
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        try {
            attendanceService.upload(file);
        } catch (IOException e) {
            return R.fail("上传失败");
        }
        return R.success();
    }

    /**
     * 保存考勤打卡记录
     */
    @PostMapping("/checkin")
    public R checkin(@RequestBody AttendanceCheckin attendanceCheckin) {

        attendanceCheckinService.checkin(attendanceCheckin);
        return R.success();
    }

    @PostMapping("checkRecord")
    public R checkRecord(@RequestBody AttendanceCheckin attendanceCheckin) {

        List<AttendanceCheckin> attendanceCheckins = attendanceCheckinService.checkRecord(attendanceCheckin);
        // 转换成attendanceData: {
        //      '2024-01-15': 'present',
        //      '2024-01-16': 'present',
        //      '2024-01-17': 'leave',
        //      '2024-01-18': 'present',
        //      '2024-01-19': 'absent'
        //    }
        // 这个格式
        Map<String, String> attendanceData = new HashMap<>();
        for (AttendanceCheckin checkin : attendanceCheckins) {
            attendanceData.put(checkin.getCheckDate().toString(), "present");
        }
        return R.success(attendanceData);
    }

}
